package com.example.administrator.hqq.adapter.discover;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.View.MyGridView;
import com.example.administrator.hqq.activity.post.ReleasePostActivity;
import com.example.administrator.hqq.adapter.SetOnClickItem;
import com.example.administrator.hqq.bean.post.PostModuleBean;
import com.example.administrator.hqq.utils.BoldTextUtils;
import com.example.administrator.hqq.utils.ScaleUtils;
import com.example.administrator.hqq.utils.TimeUtils;
import com.example.administrator.hqq.utils.transform.GlideCircleTransform;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/11/20 9:59
 * @remark:
 */
public class PostListAdapter extends BaseAdapter {
    private Context context;
    private List<PostModuleBean.DataBean> list;
    private OnClick onClick;
    private PostGridAdapter postGridAdapter;

    public PostListAdapter(Context context, List<PostModuleBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.post_list_item, viewGroup, false);
            viewHolder = new ViewHolder(view);
            if (viewHolder.data == null) {
                viewHolder.data = new ArrayList<>();
            }
            view.setTag(viewHolder);
            AutoUtils.autoSize(view);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.data.clear();
        if (list != null) {
            Log.e("kang", "接口返回+++++1:   "+list.get(i).getTitle()+"    num"+list.get(i).getPicture_url().size());
            viewHolder.data.addAll(list.get(i).getPicture_url());
        }
        viewHolder.postGridAdapter = new PostGridAdapter(context);
        viewHolder.postGridAdapter.setItemsData(viewHolder.data);
        viewHolder.postImageGrid2.setAdapter(viewHolder.postGridAdapter);
        viewHolder.postImageGrid.setAdapter(viewHolder.postGridAdapter);
        //设置群聊入口
        if (list.get(i).getGroup()==null)
        {
            viewHolder.groupEntrance.setVisibility(View.GONE);
        }else{
            viewHolder.groupEntrance.setVisibility(View.VISIBLE);
            Glide.with(context)
                    .load(list.get(i).getGroup().getUrl())
                    .bitmapTransform(new GlideCircleTransform(context)).into(viewHolder.groupImage);
            viewHolder.groupTitle.setText(list.get(i).getGroup().getTitle());
            BoldTextUtils.BoldText(viewHolder.groupTitle);
            viewHolder.ageNumber.setText(list.get(i).getGroup().getSame_age_count() + "");
            viewHolder.peopleNumber.setText(list.get(i).getGroup().getMember_amount() + "");
            viewHolder.locationNumber.setText(list.get(i).getGroup().getSame_city_count() + "");
            viewHolder.groupIntroduce.setText(list.get(i).getGroup().getIntroduction());
            if (list.get(i).getGroup().getSame_age_count()==0)
            {
                viewHolder.locationAuto.setVisibility(View.GONE);
            }else{
                viewHolder.locationAuto.setVisibility(View.VISIBLE);
            }
            if (list.get(i).getGroup().getSame_city_count()==0)
            {
                viewHolder.ageAuto.setVisibility(View.GONE);
            }else{
                viewHolder.ageAuto.setVisibility(View.VISIBLE);
            }
        }
        viewHolder.groupEntrance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClick != null) {
                    onClick.joinGroup(i);
                }
            }
        });
        //设置年龄性别属性
        if (list.get(i).getAge_type()==1) {
            viewHolder.sameAge.setVisibility(View.VISIBLE);
        }else{
            viewHolder.sameAge.setVisibility(View.GONE);
        }
        if (list.get(i).getGender_type()==0) {
            viewHolder.forumSex.setSelected(true);
            viewHolder.sexBackground.setBackgroundColor(Color.parseColor("#D67FAC"));
        }else{
            viewHolder.forumSex.setSelected(false);
            viewHolder.sexBackground.setBackgroundColor(Color.parseColor("#7F9ED6"));
        }
        //判读是否为自己的帖子
        if ("MINE".equals(list.get(i).getType()))
        {
            viewHolder.postInformrOrdelete.setImageDrawable(context.getDrawable(R.drawable.dating_delete));
        }
        else{
            viewHolder.postInformrOrdelete.setImageDrawable(context.getDrawable(R.drawable.post_inform));
        }
        viewHolder.forumImageGridAuto.setVisibility(View.VISIBLE);
        if (viewHolder.data.size() > 1) {
            if (viewHolder.data.size() == 4 || viewHolder.data.size() == 2) {

                viewHolder.postImageGrid2.setVisibility(View.VISIBLE);
                viewHolder.postImageGrid.setVisibility(View.GONE);
                viewHolder.postImage.setVisibility(View.GONE);
            } else {

                viewHolder.postImageGrid.setVisibility(View.VISIBLE);
                viewHolder.postImageGrid2.setVisibility(View.GONE);
                viewHolder.postImage.setVisibility(View.GONE);
            }
        } else if (viewHolder.data.size()==1){
            Log.e("kang", "getView+++++1:   "+list.get(i).getTitle()+"    num"+viewHolder.data.size());
            //计算对应的宽高
            ViewGroup.LayoutParams layoutParams = viewHolder.postImage.getLayoutParams();
            Log.e("Mywidth", "getView: "+layoutParams.width );
            layoutParams.width= ScaleUtils.dip2px(context,list.get(i).getPicture_large_size_width()/2);
            layoutParams.height=ScaleUtils.dip2px(context,list.get(i).getPicture_large_size_height()/2);
            Log.e("Mywidth", "getView: "+layoutParams.width );
            viewHolder.postImage.setLayoutParams(layoutParams);
            viewHolder.postImageGrid2.setVisibility(View.GONE);
            viewHolder.postImageGrid.setVisibility(View.GONE);
            viewHolder.postImage.setVisibility(View.VISIBLE);
            Glide.with(context).load(viewHolder.data.get(0).getLarge()).fitCenter().fitCenter().into(viewHolder.postImage);
        } else{
            Log.e("kang", "getView:0    "+i);
            viewHolder.forumImageGridAuto.setVisibility(View.GONE);
            viewHolder.postImage.setVisibility(View.GONE);
        }
        setOnclick(viewHolder, i);
        Glide.with(context).load(list.get(i).getUser_portrait_url()).transform(new CenterCrop(context), new GlideCircleTransform(context)).into(viewHolder.userPic);
        viewHolder.userName.setText(list.get(i).getNickname());
        viewHolder.postTitle.setVisibility(View.VISIBLE);
        if (TextUtils.isEmpty(list.get(i).getTitle()))
        {
            viewHolder.postTitle.setVisibility(View.GONE);
        }
        viewHolder.postTitle.setText(list.get(i).getTitle());
        viewHolder.postContext.setText(list.get(i).getText());
        viewHolder.postTime.setText(TimeUtils.getTimeString(list.get(i).getTime()));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClick != null) {
                    onClick.details(i);
                }
            }
        });
        //评论点赞同城
        viewHolder.local.setVisibility(list.get(i).getLocation_type() == 0 ? View.GONE : View.VISIBLE);
        viewHolder.praiseNumber.setText(list.get(i).getLike_amount() == 0 ? "赞" : list.get(i).getLike_amount() + "");
        viewHolder.postPraiseImage.setSelected(list.get(i).getLike_state() == 0 ? false : true);
        viewHolder.commentNumber.setText(list.get(i).getReply_amount() == 0 ? "评论" : list.get(i).getReply_amount() + "");
        return view;
    }

    private void setOnclick(final ViewHolder viewHolder, final int i) {
        viewHolder.postGridAdapter.setSetOnClickItem(new SetOnClickItem() {
            @Override
            public void setOnClickItem(int position) {
                if (onClick != null) {
                    onClick.lookPic(i, position);
                }
            }

            @Override
            public void setPicsOnClickItem(int position) {

            }
        });
        viewHolder.postPraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClick != null) {
                    onClick.giveLike(i, viewHolder.postPraiseImage, viewHolder.praiseNumber);
                }
            }
        });
        viewHolder.report.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClick != null) {
                    onClick.report(i);
                }
            }
        });
        viewHolder.postImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClick != null) {
                    onClick.lookPic(i, 0);
                }
            }
        });
    }


    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public interface OnClick {
        void lookPic(int i, int i1);

        void giveLike(int i, ImageView image, TextView textVIew);

        void details(int i);

        void report(int i);

        void joinGroup(int position);
    }

    static class ViewHolder {
        @BindView(R.id.user_pic)
        ImageView userPic;
        @BindView(R.id.user_name)
        TextView userName;
        @BindView(R.id.local)
        TextView local;
        @BindView(R.id.report)
        AutoLinearLayout report;
        @BindView(R.id.post_title)
        TextView postTitle;
        @BindView(R.id.post_context)
        TextView postContext;
        @BindView(R.id.post_image_grid)
        MyGridView postImageGrid;
        @BindView(R.id.post_image_grid2)
        MyGridView postImageGrid2;
        @BindView(R.id.post_image)
        ImageView postImage;
        @BindView(R.id.post_time)
        TextView postTime;
        @BindView(R.id.comment_number)
        TextView commentNumber;
        @BindView(R.id.post_comment)
        AutoLinearLayout postComment;
        @BindView(R.id.praise_number)
        TextView praiseNumber;
        @BindView(R.id.post_praise)
        AutoLinearLayout postPraise;
        @BindView(R.id.post_praise_image)
        ImageView postPraiseImage;
        @BindView(R.id.post_informrOrdelete)
        ImageView postInformrOrdelete;
        @BindView(R.id.forum_sex)
        ImageView forumSex;
        @BindView(R.id.sex_background)
        AutoRelativeLayout sexBackground;
        @BindView(R.id.same_age)
        TextView sameAge;
        @BindView(R.id.group_image)
        ImageView groupImage;
        @BindView(R.id.group_title)
        TextView groupTitle;
        @BindView(R.id.people_number)
        TextView peopleNumber;
        @BindView(R.id.number_auto)
        AutoLinearLayout numberAuto;
        @BindView(R.id.group_introduce)
        TextView groupIntroduce;
        @BindView(R.id.group_entrance)
        AutoRelativeLayout groupEntrance;
        @BindView(R.id.forum_image_grid_auto)
        AutoRelativeLayout forumImageGridAuto;
        @BindView(R.id.age_number)
        TextView ageNumber;
        @BindView(R.id.age_auto)
        AutoLinearLayout ageAuto;
        @BindView(R.id.location_number)
        TextView locationNumber;
        @BindView(R.id.location_auto)
        AutoLinearLayout locationAuto;
        private List<PostModuleBean.DataBean.PictureUrlBean> data;
        private PostGridAdapter postGridAdapter;
        private int index=-1;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
